package com.xpgk.xpsoft.code;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.common.controller.BaseController;
import com.xpgk.xpsoft.user.UserService;

public class CodeTypeController extends BaseController {
	public void index() {
		keepPara();
		Kv cond = Kv.by("type = ", getPara("type")).set("note like ",
				isParaBlank("note") ? null : getPara("note", "") + "%");
		Page<Record> pages = Db.paginate(getParaToInt("pageNum", 1), 10,
				Db.getSqlPara("xpSoftCode.findTypeList", Kv.by("cond", cond)));

		// //Map<Integer, String> map=PubKit.toMap(userList, "id", "userName");
		// //PubKit.addColumnTo(pages.getList(), "userID", map, "userName");
		setAttr("lists", pages);
		setAttr("bCanAdd", UserService.hasRight(getCurUserID(), "/xpCodeType/add_ui"));
		setAttr("bCanEdit", UserService.hasRight(getCurUserID(), "/xpCodeType/edit_ui"));
		render("index.html");
		// t.OutputDelta();
	}

	// 注：定义函数名时，如"void add()",页面的展示请以_ui结尾，void add_ui() 涉及到权限问题
	// add与add_ui有同样的权限
	public void add_ui() {
		render("add.html");
	}

	// 注册
	@Before(Tx.class)
	public void add() {
		keepPara();
		Record type = GetRecord("type");
		String result = CodeTypeService.saveType(type);
		if (!result.equals("ok"))
			renderMsg(result, "add.html");
		else
			renderMsg("添加成功.", "add.html", true);
	}

	// 修改
	public void edit_ui() {
		int id = getParaToInt("id", 0);
		Record type = Db.findById("frm_code_type", id);
		setAttr("type", type);
		setAttr("bEdit", 1);
		render("edit.html");
	}

	// 修改
	@Before(Tx.class)
	public void edit() {
		Record type = GetRecord("type");
		keepPara();
		setAttr("bEdit", 1);
		String result = CodeTypeService.updateType(type);
		if (!result.equals("ok")) {
			renderMsg(result, "edit.html");
			return;
		}
		renderMsg("修改成功.", "edit.html", true);
	}
}
